<?php
/**
 * Created by PhpStorm.
 * User: JX
 * Date: 2017/8/7
 * Time: 14:40
 */

namespace App\Http\Controllers\API\WeChat;

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use App\Models\WeChat;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class WeChatController extends Controller
{
    public function index(Request $request)
    {
        $user = Auth::user();
        if($user->hasRole('singer')){
            $wechatlist = DB::table('user_wechats')->where(['user_id'=>$user->id])->pluck('wechat_id')->toArray();
        }else{
            $wechatlist = WeChat::query()->pluck('id');
        }
        $queryString = $request->get('query', '');
        $models = WeChat::query()
            ->where('nick_name', 'like', '%' . $queryString . '%')
            ->whereIn('id', $wechatlist)
            ->paginate();
        return $models;
    }
    public function getList(){
        $user = Auth::user();
        $wechat = DB::table('user_wechats')->where(['user_id'=>$user->id])->pluck('wechat_id')->toArray();
        $result=[];
        if(!$user->hasRole('singer')){
            $companys = WeChat::query()->get();
        }else{
            $companys = WeChat::query()->whereIn('id',$wechat)->get();
        }
        if($companys){
            foreach ($companys as $items){
                $result[$items->id]=$items->nick_name;
            }
        }
        return $result;
    }
    public function getDefault(){
        $user = Auth::user();
        $wechat = DB::table('user_wechats')->where(['user_id'=>$user->id])->pluck('wechat_id')->toArray();
        if(!$user->hasRole('singer')) {
            $default = WeChat::query()->first();
        }else{
            $default =WeChat::query()->whereIn('id',$wechat)->first();
        }
        if($default){
            $result['id'] = $default->id;
            $result['name'] = $default->nick_name;
        }else{
            $result['id'] = 0;
            $result['name'] = '尚未添加';
        }
        return $result;
    }
    public function type()
    {
        return WeChat::$types;
    }


    /**
     * 添加公众号
     * @param Request $request
     * @return mixed
     */
    public function store(Request $request)
    {
        $model = new WeChat();
        $model->fill($request->all());
        if ($model->save()) {
            return $model;
        } else {
            return response($model->getErrors()->toArray(), 406);
        }
    }


    /**
     * 修改员工
     * @param User $User
     * @param Request $request
     * @return mixed
     */
    public function update(WeChat $WeChat, Request $request)
    {
        $WeChat->fill($request->all());
        if ($WeChat->save()) {
            return $WeChat;
        } else {
            return response($WeChat->getErrors()->toArray(), 406);
        }
    }


    /**
     * 删除
     * @param $id
     * @return mixed
     */
    public function destroy($id)
    {
        $model = $this->findModel($id);
        try {
            $model->delete();
            return ['state' => 'success'];
        } catch (\Exception $e) {
            return ['state' => 'failed', 'message' => $e->getMessage()];
        }
    }


    /**
     * 返回单个实例
     * @param User $user
     * @return mixed
     */
    public function show($id)
    {
        return $this->findModel($id);
    }




    /**
     * @param $id
     * @return Staff
     */
    public function findModel($id)
    {
        $model = WeChat::findOrFail($id);
        return $model;
    }
}